package com.sike.controller;

import com.sike.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author vito
 * @version 1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 更新用户余额
    @PatchMapping("/balance/{userId}")
    public ResponseEntity<?> updateBalance(@PathVariable Integer userId, @RequestBody BigDecimal balance) {
        try {
            userService.updateBalance(userId, balance);
            return ResponseEntity.ok("余额更新成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
        }

    // 获取用户余额
    @GetMapping("/balance")
    public ResponseEntity<?> getBalance() {
        try {
            Integer userId = 1;
            BigDecimal balance = userService.getBalance(userId);
            return ResponseEntity.ok(balance);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}
